/*
AlliedBridge Content Management System
Copyright (C) 2006 AlliedBridge

This program is free software; you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software Foundation;

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.volume4.alliedbridge.model.person;

import java.io.*;
import java.sql.*;
import java.util.Iterator;
import java.util.List;

import javax.servlet.*;
import javax.servlet.http.*;

import org.volume4.utils.ServletUtilities;

public class ContactManager extends HttpServlet {
	
	public String DRIVER,URL,USER,PASS,fieldName,fieldData,fileName,fileFieldName;	
	public String app_root,messageSuccess,messageFailed,postal_address,physical_address,
	phone,fax,cellular,email,website;

	public void init() throws ServletException {
		ServletContext context = getServletContext();
		DRIVER = context.getInitParameter("driver");
		URL = context.getInitParameter("mc_dburl");
		USER = context.getInitParameter("mc_dbuser");
		PASS = context.getInitParameter("mc_dbpass");
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html");		
		Connection con = null;
		// These need to be moved to a properties file.
		messageSuccess = "Information successfuly updated.";
		messageFailed = "Update operation failed.";

		try {	            
	            Class.forName(DRIVER);
				con = DriverManager.getConnection(URL,USER,PASS);
				
				postal_address = request.getParameter("postal_address");
				physical_address = request.getParameter("physical_address");
				phone = request.getParameter("phone");
				fax = request.getParameter("fax");
				cellular = request.getParameter("cellular");
				email = request.getParameter("email");
				website = request.getParameter("website");
				
				String query = "UPDATE ab_address_info SET " +						
						"postal_address= '" + postal_address + "', " +
						"physical_address= '" + physical_address + "', " +
						"phone= '" + phone + "', " +
						"fax= '" + fax + "', " + 
						"cellular= '" + cellular + "', " + 
						"email= '" + email + "', " + 
						"website= '" + website + "'";						

				PreparedStatement stmt = con.prepareStatement(query);

				int result = stmt.executeUpdate(query);	            

				app_root = request.getContextPath();
				response.sendRedirect(app_root + "/application/application_messages/process_result.jsp?message=" + messageSuccess);	            
	           
		} catch (SQLException ex) {
			System.out.println("\nERROR:------ SQLException -----\n");
			while (ex != null) {
				System.out.println("Message: " + ex.getMessage());
				System.out.println("SQLState: " + ex.getSQLState());
				System.out.println("ErrorCode :" + ex.getErrorCode());
				ex = ex.getNextException();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (con != null)
					con.close();
			} catch (SQLException ex) {
				System.out.println("\nERROR:------ SQLException -----\n");
				System.out.println("Message: " + ex.getMessage());
				System.out.println("SQLState: " + ex.getSQLState());
				System.out.println("ErrorCode :" + ex.getErrorCode());
			}
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}
}